package ua.kharkov.knure.pereverziev.diploma.web.command;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.pereverziev.diploma.db.DAOServices.ServiceFactory;
import ua.kharkov.knure.pereverziev.diploma.db.entity.Route;
import ua.kharkov.knure.pereverziev.diploma.db.entity.Ticket;
import ua.kharkov.knure.pereverziev.diploma.db.entity.User;

public class CreateTicketCommand implements Command {
	private static final Logger log = Logger
			.getLogger(CreateTicketCommand.class);

	@Override
	public String execute(final HttpServletRequest request,
			final HttpServletResponse response) throws IOException,
			ServletException {

		final HttpSession session = request.getSession();

		final String route = request.getParameter("route");
		log.debug("Request parameter: route --> " + route);

		final String direction = request.getParameter("direction");
		log.debug("Request parameter: direction --> " + direction);
		String forward = "/WEB-INF/jsp/error_page.jsp";

		if (route != null) {

			final Route currentRoute;
			final Ticket ticket = new Ticket();
			ticket.setPurchaseDate(new java.sql.Date(Calendar.getInstance()
					.getTime().getTime()));
			@SuppressWarnings("unchecked")
			final Map<String, String[]> params = request.getParameterMap();

			for (final String key : params.keySet()) {
				final String service = (params.get(key))[0];
				log.debug("-----------> " + key + "~" + service);
				if (key.contains("service")) {
					log.debug("Found service id= "
							+ Long.parseLong(service.split("_")[0]));
					ticket.getServices()
							.add(ServiceFactory
									.getInstance()
									.getAdditionalServService()
									.load(Long.parseLong(service.split("_")[0])));
				}
			}

			if ("2".equals(direction)) {

				currentRoute = ((List<Route>) session
						.getAttribute("returnFlights")).get(Integer
						.parseInt(route));
				ticket.setUser((User) session.getAttribute("user"));
				ticket.setFlights(currentRoute.getFlights());
				ticket.generatePrice();

				forward = "/WEB-INF/jsp/priceCalculation.jsp";
				session.setAttribute("returnTicket", ticket);
			} else if ("1".equals(direction)) {
				currentRoute = ((List<Route>) session
						.getAttribute("directFlights")).get(Integer
						.parseInt(route));
				ticket.setUser((User) session.getAttribute("user"));
				ticket.setFlights(currentRoute.getFlights());
				ticket.generatePrice();
				session.setAttribute("directTicket", ticket);
				if (session.getAttribute("type").equals("single")) {
					forward = "/WEB-INF/jsp/priceCalculation.jsp";
				} else {
					forward = "/WEB-INF/jsp/flightChoice.jsp?direction=2";
				}
			} else
				forward = "/WEB-INF/jsp/error_page.jsp";
		}
		session.setAttribute("lastPage", forward);
		return forward;
	}
}
